package com.imiku.controller;

import com.alibaba.fastjson.JSONObject;
import com.imiku.model.RoleInfo;
import com.imiku.service.RoleInfoService;
import com.imiku.utils.JsonUtils;
import com.imiku.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/role")
public class RoleInfoController {

    @Autowired
    private RoleInfoService roleInfoService;

    @RequestMapping("/findByPage")
    public String findByPage(PageVo pageVo) {
        Page<RoleInfo> users = roleInfoService.findByPage(pageVo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "success");
        jsonObject.put("page", users);
        return jsonObject.toJSONString();
    }

    @RequestMapping("/details/{roleId}")
    public String details(@PathVariable int roleId) {
        RoleInfo roleInfo = roleInfoService.findByRoleId(roleId);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        jsonObject.put("info", roleInfo);
        return jsonObject.toJSONString();
    }


    @RequestMapping("/save")
    public String save(RoleInfo roleInfo) {
        roleInfoService.save(roleInfo);
        JSONObject jsonObject = JsonUtils.getJsonObject(1, "成功");
        return jsonObject.toJSONString();
    }

}
